<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *模板管理
*/
defined('BY_JYA') or exit('error');
$install = check_permission('jy_weishop');
if(!$install){
	exi('请先安装智慧电商客','error');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'模板列表'),
	array('op'=>'edit','title'=>'模板详情'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_weishop_diyform_temp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['group_title']=pdo_getcolumn('jy_weishop_diyform_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['groupid']),'title');

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
    $aitip='每个商品，可以选择不一样的表单模板！';
    if($_SESSION['isfounder']){
        $aitip.='</br>【注意】如果字段是身份证类型，且安装了《天眼数聚身份证实名接口》，接口开启后，会判断身份证二要素（管理员可见）！：
		';
    }
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'模板列表'),
			array('op'=>'edit','title'=>'添加模板'),
		),
		'list'=>array('data'=>'list','tip'=>$aitip,'edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'模板id','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'group_title','title'=>'分类','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'模板标题','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_diyform_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['diyformfields'] =json_decode($item['diyformfields'],true);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls[]=array(
		'type'=>'look',
		'width'=>'350px',
		'flex'=>1,
		'look'=>array(
				array('type'=>'diyform','title'=>'模板名称','name'=>'diyformfields'),
			),
	);
	$htmls[]=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
					array('type'=>'string','title'=>'模板名称','name'=>'title','help'=>''),
					array('name'=>'groupid','title'=>'所属分类','type'=>'select','options'=>'groups','option_title'=>'title'),
					array(
							'type'=>'array',
							'name'=>'diyformfields',
							'add_tip'=>'添加自定义表单项',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
									array('type'=>'switch','name'=>'ismust','title'=>'*必填?'),
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array(
											'type'=>'array_simple',
											'show'=>array('type'=>array(4,5,17,19)),
											'name'=>'value',
											'add_tip'=>'添加自定义表单项',
												'fields'=>array(
													array('type'=>'string','name'=>'title','title'=>'选项名称','help'=>''),
												),
									),
									array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
									array('type'=>'switch','name'=>'ismust','title'=>'*必填?'),
								)
					),
					
		)
	);
	$return['types']=get_form_types();
	$return['groups']=pdo_getall('jy_weishop_diyform_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'groupid'=>intval($postdata['groupid']),
		'diyformfields'=>ijson_encode($postdata['diyformfields']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_diyform_temp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_diyform_temp',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_diyform_temp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}